package org.alex.samples.dao;

import org.alex.samples.javabean.ExamScoreVO;
import org.alex.samples.model.ExamScore;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

@Repository
public class ExamScoreDAOImpl extends BaseHibernateDao<ExamScore> implements IExamScoreDAO {

	@Override
	public ExamScore getScore(String userId, Integer subId) {
		String hql = "from ExamScore es where es.userId = :userId and es.subId = :subId";
		ExamScore examScore = (ExamScore) this.createQuery(hql).setString("userId", userId).setInteger("subId", subId)
				.uniqueResult();
		
		if (examScore != null) {
			return examScore;
		}
		return null;
	}

	@Override
	public ExamScoreVO getScoreById(Integer scoreId) {
		StringBuilder hql = new StringBuilder();
		hql.append(
				" select user.name as userName, sub.name as subjectName, es.examTime as examTime, es.score as score");
		hql.append(" from ExamScore es, ExamUser user, ExamSubject sub ");
		hql.append(" where es.userId  = user.id ");
		hql.append(" and es.subId = sub.id");
		hql.append(" and es.id = :scoreId");
		return (ExamScoreVO) this.createQuery(hql.toString())
				.setResultTransformer(Transformers.aliasToBean(ExamScoreVO.class)).setInteger("scoreId", scoreId)
				.uniqueResult();
	}

}
